(function(window,document,$){

// 页面适配
var $win = $(window),
	$doc = $(document);

var G = {
	width : 0,height : 0,left : 0,top : 0,
	gameW : 0,gameH : 0,
	resize : function($cur,h,w,$p){
		var winWidth,winHeight;
		// w = 5,h = 9;
		/**
		 * 获取得到窗口尺寸
		 */
		if($p){
			winWidth = $p.width();
			winHeight = $p.height();
		}else{
			winWidth = $win.width();
			winHeight = $win.height();
		}
		var width,height,left,top;
		// winWidth/winHeight = h/w => winWidth*w = winHeight*h
		if(winWidth*w > winHeight*h){
			// 如果宽度太宽:以高度来计算
			width = (winHeight*h)/w;
			height = winHeight;
			left = (winWidth-width)/2;
			top = 0;
		}else{
			// 如果高度太高:以宽度来计算
			width = winWidth;
			height = (winWidth*w)/h;
			left = 0;
			top = (winHeight-height)/2;
		}
		if(typeof $cur === 'string'){
			$cur = $($cur);
		}
		G.width=width;
		G.height=height;
		G.left=left;
		G.top=top;
		$cur.css({'margin-left' : left,'margin-top' : top,width : width,height : height});
	},
	onresize : function($cur,h,w,$p){
		G.gameW=h;
		G.gameH=w;
		G.resize($cur,h,w,$p);
		$win.resize(function(){
			G.resize($cur,h,w,$p);
		});
	},
	randomNum:function(min,max){
		return Math.floor( Math.random()*(max-min+1)+min );
	},
	event_offset:function(e,w,h){
		var tempx = e.offsetX==null?e.layerX:e.offsetX;
		var tempy = e.offsetY==null?e.layerY:e.offsetY;
		var offsetX = tempx*w/G.width;
		var offsetY = tempy*h/G.height;
		return {offsetX:offsetX,offsetY:offsetY};
	},
	// 判断两个坐标是否相同
	equals:function(x1,y1,x2,y2){
		return x1==x2 && y1==y2;
	},
	// 获取两个坐标直接的距离
	distance:function(x1,y1,x2,y2){
		return Math.sqrt( Math.pow(x1-x2,2) + Math.pow(y1-y2,2) );
	},
	// 点碰撞矩形检测
	point_rect:function(x1,y1,x2,y2,w2,h2){
		return x1>x2 && x1<x2+w2 && y1>y2 && y1<y2+h2;
	},
	// 矩形碰撞矩形检测【逆推】
	rect_rect:function(x1,y1,w1,h1,x2,y2,w2,h2){
		return x1<=x2+w2 && x2<=x1+w1 && y1<=y2+h2 && y2<=y1+h1;
	},
	// 矩形碰撞圆形检测【逆推】
	rect_circ:function(x1,y1,w1,h1,x2,y2,r2){
		return x2-r2<=x1+w1 && x1<=x2+r2 && y2-r2<=y1+h1 && y1<=y2+r2;
	},
	// 点碰撞圆形检测
	point_circ:function(x1,y1,x2,y2,r2){
		return G.distance(x1,y1,x2,y2) < r2;
	},
	// 圆形碰撞圆形检测
	circ_circ:function(x1,y1,r1,x2,y2,r2){
		return G.distance(x1,y1,x2,y2) <= r1+r2;
	},
	// 绘制圆角矩形
	drawRoundRect:function(c,pa){
		c.beginPath();
		c.moveTo(pa[0] + pa[4], pa[1]);
		c.lineTo(pa[0] + pa[2] - pa[4], pa[1]);
		c.arcTo(pa[0] + pa[2], pa[1], pa[0] + pa[2], pa[1] + pa[4], pa[4]);
		c.lineTo(pa[0] + pa[2], pa[1] + pa[3] - pa[4]);
		c.arcTo(pa[0] + pa[2], pa[1] + pa[3], pa[0] + pa[2] - pa[4], pa[1] + pa[3], pa[4]);
		c.lineTo(pa[0] + pa[4], pa[1] + pa[3]);
		c.arcTo(pa[0], pa[1] + pa[3], pa[0], pa[1] + pa[3] - pa[4], pa[4]);
		c.lineTo(pa[0], pa[1] + pa[4]);
		c.arcTo(pa[0], pa[1], pa[0] + pa[4], pa[1], pa[4]);
		c.closePath();
	},
	// 为 CanvasRenderingContext2D 增加属性和方法
	canvasRenderingContext2D:function(options){
		G.override(CanvasRenderingContext2D.prototype,options);
	}
};

/**
 * 加载类
 */
var Load=function(){
	var s = this;

	s.medias = {}; // 媒体对象
	s.progress = 0; // 进度(小数)
	s.index = 0; // 当前进度(索引)
	s.total = 0; // 总进度(总长度)
};
Load.prototype={
	media : function(res,progress,result){
		var s = this,i = 0,temp,pgStatus = (typeof progress === 'function');

		var imgl = 0,audiol = 0,imgs = res.img || [],audios = res.audio || [];
		if(imgs){
			imgl = imgs.length;
		}
		if(audios){
			audiol = audios.length;
		}

		s.total = imgl+audiol;

		var resLoad = function(){
			s.index++;
			s.progress = s.index/s.total;

			if(pgStatus){
				progress(s.progress);
			}
			if(s.index>=s.total){
				// 最后一张图片
				if(typeof result==='function'){
					resLoad = null;
					result(s.medias);
				}
			}
		}

		// 无资源加载
		if(s.total<=0){
			if(typeof result==='function'){
				resLoad = null;
				result(s.medias);
			}
			return;
		}

		for(i = 0;i < imgl;i++){
			temp = new Image();
			s.medias[ imgs[i]['name'] ] = temp;
			temp.src = imgs[i]['path'];
			if(temp.complete){
				resLoad();
				continue;
			}
			temp.onload = resLoad;
			temp.onerror = resLoad;
			temp.id=imgs[i]['name'];
		}

		for(i = 0;i < audiol;i++){
			temp = document.createElement('audio');
			s.medias[ audios[i]['name'] ] = temp;
			temp.src = audios[i]['path'];
			temp.onloadeddata = resLoad;
			temp.onerror = resLoad;
		}
	}
};

G.Load=Load;

window.G = G;

})(window,document,jQuery);